package com.meiyou.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.meiyou.constant.MessageConstant;
import com.meiyou.constant.ReportStatusConstant;
import com.meiyou.context.BaseContext;
import com.meiyou.dto.HandleReportDto;
import com.meiyou.dto.ReportArticleDto;
import com.meiyou.dto.ReportPageDto;
import com.meiyou.dto.StarDto;
import com.meiyou.entity.Report;
import com.meiyou.entity.Star;
import com.meiyou.exception.NoPermissionException;
import com.meiyou.exception.ReportExistException;
import com.meiyou.mapper.ArticleMapper;
import com.meiyou.mapper.ReportMapper;
import com.meiyou.mapper.StarMapper;
import com.meiyou.mapper.UserMapper;
import com.meiyou.result.PageResult;
import com.meiyou.service.ReportService;
import com.meiyou.service.StarService;
import com.meiyou.vo.ArticleVo;
import com.meiyou.vo.ReportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.service.impl
 * @Author: 尹晓安
 * @Date: 2023/11/25 18:10
 * @Description: todo
 */
@Service
public class ReportServiceImp implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 举报文章
     *
     * @param reportArticleDto
     * @return
     */
    @Override
    public void reportArticle(ReportArticleDto reportArticleDto) {

        Report report = new Report();

        BeanUtils.copyProperties(reportArticleDto, report);

        report.setUserId(BaseContext.getCurrentId());

        // 查询是否已经举报
        Integer count = reportMapper.select(BaseContext.getCurrentId(), reportArticleDto.getArticleId());

        if (count > 0) {

            throw new ReportExistException(MessageConstant.REPORT_EXIST);

        }

        reportMapper.insert(report);

    }

    /**
     * 处理举报
     *
     * @return
     */
    @Override
    public void handleReport(HandleReportDto handleReportDto) {

        // 1.判断当前用户是否是管理员
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        Integer role = userMapper.selectRole(userId);
        // role等于0位普通用户，没有权限
        if (role == 0) {
            throw new NoPermissionException(MessageConstant.NO_PERMISSION);
        }

        // 删除举报
        reportMapper.delete(handleReportDto.getId());

        // 1通过 0拒绝
        Integer status = handleReportDto.getStatus();

        // 如果是通过就删除举报的那篇文章，反之不删除
        if (status == ReportStatusConstant.REPORT_PASS) {

            articleMapper.deleteById(handleReportDto.getArticleId());

        }

    }

    /**
     * 分页获取举报列表
     *
     * @return
     */
    @Override
    public PageResult getReportPage(ReportPageDto reportPageDto) {

        // 启动分页查询
        PageHelper.startPage(reportPageDto.getPage(), reportPageDto.getPageSize());

        Page<ReportVo> reportVo = reportMapper.getReportPage();

        return new PageResult(reportVo.getTotal(), reportVo.getResult());

    }
}
